package Concurent;

import java.util.concurrent.Semaphore;

/**
 * Created by root on 17-6-8.
 */
public class SemaphoreDemo1 {
    //协调线程的
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3,true); //三张票,后面加个true就是保证公平
        for(int i = 0;i<Integer.MAX_VALUE;i++) {
            new Thread(new T11(semaphore, i)).start();
        }
    }
}

class T11 implements Runnable{
    private Semaphore semaphore = null;
    private int i;
    public T11(Semaphore semaphore,int i) {
        this.semaphore = semaphore;
        this.i = i;
    }


    @Override
    public void run() {
        for (int i=0;i<Integer.MAX_VALUE;i++){
            try {
                semaphore.acquire();
                System.out.println("游客"+i+"得到了票");
                Thread.sleep(i*1000);
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}